<?php

namespace App\Services\ApplyGoldRecordBatchPay;

use App\Models\User;
use App\Jobs\ApplyGoldProductPayingJob;

class ApplyGoldRecordBatchPay
{
	protected User $user;

	protected array $unpaidCacheIds = [];

	public function __construct(User $user, $unpaidCacheIds)
	{
		$this->user = $user;
		$this->unpaidCacheIds = $unpaidCacheIds;
	}

	public function pay()
	{
		if (count($this->unpaidCacheIds) <= 0) return;

		foreach ($this->unpaidCacheIds as $cacheId) {
            $this->user->markRecordPayCachePaying($cacheId);
            $queue = app()->environment('local') ? 'default' : 'pay_gold_record';
            dispatch(new ApplyGoldProductPayingJob($this->user, $cacheId))->onQueue($queue);
		}
	}
}
